//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.comprosoft.telnet.io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

public class IOHandler {
    private Socket socket;
    private final InputStreamReader input_reader;
    private final BufferedReader reader;
    private final PrintWriter out;
    private TextColor txtColor;
    private BackColor bgColor;

    public IOHandler(Socket socket) throws IOException {
        this.txtColor = TextColor.WHITE;
        this.socket = socket;
        this.input_reader = new InputStreamReader(socket.getInputStream(), "GBK");
        this.reader = new BufferedReader(this.input_reader);
        this.out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "GBK"), true);
        this.SetTextColor(this.txtColor);
    }

    public String readLine() throws IOException {
        return this.readLine("");
    }

    public String readLine(String prompt) throws IOException {
        String retStr = "";
        int tempChar = 0;
        this.Write(prompt);
        this.Flush();

        while((char)tempChar != '\n' && !Thread.currentThread().isInterrupted()) {
                while(this.reader.ready()) {
                        tempChar = this.reader.read();
                        if (tempChar > 31 && tempChar < 127) {
                            retStr = retStr + (char)tempChar;
                        } else if (tempChar == 27) {
                            this.flushSpecialChar();
                        } else if (tempChar == 8) {
                            this.socket.sendUrgentData(32);
                            this.socket.sendUrgentData(8);
                            retStr = this.backspace(retStr);
                        } else if (tempChar != 10 && tempChar != 13) {
                            retStr = retStr + (char)tempChar;
                        } else if (tempChar == 10) {
                        }
                    }
                }



        return retStr;
    }

    private String backspace(String str) {
        if (str != null && str.length() > 0) {
            str = str.substring(0, str.length() - 1);
        }

        return str;
    }

    private int flushSpecialChar() throws IOException {
        this.reader.read();
        return this.reader.read();
    }

    public void SetTextColor(TextColor c) {
        this.txtColor = c;
        this.out.print(c.GetEscapeCode());
        this.out.flush();
    }

    public void SetBackgroundColor(BackColor c) {
        this.bgColor = c;
        this.out.println(c.GetEscapeCode());
        this.out.flush();
    }

    public TextColor GetTextColor() {
        return this.txtColor;
    }

    public BackColor GetBackgroundColor() {
        return this.bgColor;
    }

    public void Write(String input) {
        this.out.print(input);
    }

    public void WriteLine(String input) {
        this.out.println(input);
        this.out.flush();
    }

    public void ClearScreen() {
        this.out.print("\u001b[2J");
        this.out.flush();
    }

    public void Flush() {
        this.out.flush();
    }

    public void destroy() {
        try {
            this.reader.close();
            this.out.close();
        } catch (Exception var2) {
            var2.printStackTrace();
        }

    }

    public static void main(String[] args) {
        System.out.println('\b');
    }
}
